package itman.elm.service.impl;

import itman.elm.dao.BusinessDao;
import itman.elm.dao.CartDao;
import itman.elm.dao.OrderDetailetDao;
import itman.elm.dao.OrdersDao;
import itman.elm.dao.impl.BusinessDaoImpl;
import itman.elm.dao.impl.CartDaoImpl;
import itman.elm.dao.impl.OrderDetailetDaoImpl;
import itman.elm.dao.impl.OrdersDaoImpl;
import itman.elm.po.Business;
import itman.elm.po.Cart;
import itman.elm.po.OrderDetailet;
import itman.elm.po.Orders;
import itman.elm.service.OrdersService;
import itman.elm.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public int createOrder(String userId, Integer businessId, Integer daId) {

        int orderId = 0;
        BusinessDao businessDao = new BusinessDaoImpl();
        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try{
            Cart cart = new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);

            List<Cart> cartList = cartDao.listCart(cart);
            Business business = businessDao.getBusinessById(businessId);

            //3、获取订单总额
            Double ordersTotal = 0.0;
            for(Cart c : cartList){
                ordersTotal += c.getFood().getFoodPrice()*c.getQuantity();
            }

            //加上配送费
            ordersTotal += business.getDeliveryPrice();

            //创建订单，获取订单编号
            Orders orders = new Orders();
            orders.setUserId(userId);
            orders.setBusinessId(businessId);
            orders.setOrderTotal(ordersTotal);
            orders.setDaId(daId);
            orderId = ordersDao.saveOrders(orders);

            //4、处理相关数据：获取订单明细集合
            List<OrderDetailet> orderDetailetList = new ArrayList<>();  //订单明细集合
            for(Cart c : cartList) {
                OrderDetailet od = new OrderDetailet();
                od.setOrderId(orderId);
                od.setFoodId(c.getFoodId());
                od.setQuantity(c.getQuantity());
                orderDetailetList.add(od);
            }

            //5、批量添加订单明细
            orderDetailetDao.saveOrderDetailetBatch(orderDetailetList);
            //6、清空当前用户购物车中当前商家的所有食品
            cartDao.removeCart(cart);
            DBUtil.commitTransaction();

        } catch (Exception e){
            orderId = 0;
            try {
                DBUtil.rollbackTransaction();;
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDao dao = new OrdersDaoImpl();
        try {
            DBUtil.getConnection();
            orders = dao.getOrdersById(orderId);
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close();

        }
        return orders;
    }

    @Override
    public List<Orders> listOrderByUserId(String userId) {
        List<Orders> list = new ArrayList<>();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try {
            DBUtil.getConnection();
            list = ordersDao.listOrdersById(userId);
            for (Orders o : list){
                List<OrderDetailet> odList = orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
                o.setOdList(odList);

            }
            DBUtil.commitTransaction();;

        }catch (Exception e){
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}
